
var map = null;
var geocoder = null;
 
 	var map;
	var pos;
	var i=0;
	var color;
	var Mylocation1;
	var Mylocation2;
	var file_xml ="admin/data/xmlv2.xml"; 
	var homexml = "admin/data/home.xml"; 
	var file_list="admin/data/list.xml";
	

	
function getHome()
	{
	$.ajax({url:homexml, cache:false, success: function(data)
	 {
		   $(data).find("Home").each(function() 
		   {
				Mylocation1 = parseFloat($(this).find("lat").text());
				Mylocation2 = parseFloat($(this).find("lng").text());
				
				var myLatlng = new google.maps.LatLng(Mylocation1,Mylocation2); 
				var myOptions = {
					zoom: 16,
					center: myLatlng,
					mapTypeId: google.maps.MapTypeId.ROADMAP,
					mapTypeControl:true, 
					mapTypeControlOptions: {
						style:google.maps.MapTypeControlStyle.DROPDOWN_MENU,
						position:google.maps.ControlPosition.TOP_CENTER
					}
				}
				map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
				
			});//end each
	 }//end function data
	});//end ajax	
	 //return false;
	}//end getHome
	
	
function getPosition(pos)
	{
	var bounds = new google.maps.LatLngBounds();
	$("#tab > tbody").empty();
	$.ajax({url:file_xml, cache:false, success: function(data) 
	 {
		   $(data).find("Position").each(function() 
		   {
				
			   
				var type =  $(this).attr("type"); //get Type
				
				if (i%2 == 0)
					color = "#FFFFFF";
				else
					color = "#EEEEEE";
				if (type == pos)
				{
					 var company = $(this).find("company").text(); //get Company
					  var lat = $(this).find("lat").text();
					  var lng= $(this).find("lng").text();
					  var add= $(this).find("add").text();
					  var image= $(this).find("image").text();
					  var des= $(this).find("des").text();
					  var url= $(this).find("url").text();
					  var icon= 'admin/icon/'+$(this).find("icon").text();
					  var latlng = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
					  
					  
					  var elem = $('<tr class="add" bgcolor="'+color+'"><td><a href="abc.com" onclick ="infoOpen('+lat+','+lng+');" class="linkgmap">'+company+'</a></td> <td>'+add+'</td></tr>');
					  $("#tab").append(elem);
					  
					  $(".linkgmap").click(function(e) {
                        e.preventDefault();
                    });
					  
					  var content='<div class="content">'+
						'<p id="firstHeading" class="firstHeading">'+ company +'</p>'+
						'<div id="bodyContent">'+
						'<img src="admin/image/'+image+'" class="img" width="100px" height="100px" />'+
						'<span class="text">'+
						des+'<br>'+'Add: '+add+'<br>'+'Link: '+'<a href="'+url+'">'+url+'</a>'+
						'</span></div></div>';
					 
					  bounds.extend(latlng);
			
					 //var  marker = new google.maps.Marker({ position: latlng,  map: map });
					var marker = new google.maps.Marker
					({
					position: latlng,
					map: map,		
					animation: google.maps.Animation.DROP,
					icon: icon,
					sidebarItem: "Position"
					});
					attachSecretMessage(marker,content);	
					
					map.fitBounds(bounds); 
					i++;

				}//end if
			});//end each
			
	 }//end function data
	});//end ajax	
	 //return false;
	}//end getPosition(pos)
	
	

function getAllPosition()
	{
	var bounds = new google.maps.LatLngBounds();
	$("#tab > tbody").empty();
	$.ajax({url:file_xml, cache:false, success: function(data) 
	 {
		   $(data).find("Position").each(function() 
		   {
				
				if (i%2 == 0)
					color = "#FFFFFF";
				else
					color = "#EEEEEE";
					
			var type =  $(this).attr("type"); //get Type
			if (type !="")
				{
					 var company = $(this).find("company").text(); //get Company
					  var lat = $(this).find("lat").text();
					  var lng= $(this).find("lng").text();
					  var add= $(this).find("add").text();
					  var image= $(this).find("image").text();
					  var des= $(this).find("des").text();
					  var url= $(this).find("url").text();
					  var icon= 'admin/icon/'+$(this).find("icon").text();
					  var latlng = new google.maps.LatLng(parseFloat(lat),parseFloat(lng));
					  
					  
					  var elem = $('<tr class="add" bgcolor="'+color+'"><td><a href="#" onclick ="infoOpen('+lat+','+lng+');" >'+company+'</a></td> <td>'+add+'</td> </tr>');
					  $("#tab").append(elem);
					  
		  
					  
					  var content='<div class="content">'+
						'<p id="firstHeading" class="firstHeading">'+ company +'</p>'+
						'<div id="bodyContent">'+
						'<img src="admin/image/'+image+'" class="img" width="100px" height="100px" />'+
						'<span class="text">'+
						des+'<br>'+'Add: '+add+'<br>'+'Link: '+'<a href="'+url+'">'+url+'</a>'+
						'</span></div></div>';
					 
					  bounds.extend(latlng);
			
					 //var  marker = new google.maps.Marker({ position: latlng,  map: map });
					var marker = new google.maps.Marker
					({
					position: latlng,
					map: map,		
					animation: google.maps.Animation.DROP,
					icon: icon,
					sidebarItem: "Position"
					});
					attachSecretMessage(marker,content);	
					
					map.fitBounds(bounds); 
					i++;
				}//end if
			});//end each
			
	 }//end function data
	});//end ajax	
	 //return false;
	}//end getPosition(pos)


	
function infoOpen(lat,lng)
{
	var myLatLng = new google.maps.LatLng( lat, lng );
	
	var marker = new google.maps.Marker({
					 position: myLatLng,
					 map: map,
					 animation: google.maps.Animation.DROP,
					 title:'HERE',
					 icon:'admin/icon/down.png',
					 optimized: true, 
    				 zIndex: -5 
				  });
				  	
	marker.setPosition( new google.maps.LatLng( lat, lng ) );
	map.panTo(new google.maps.LatLng( lat, lng ));
}	


function attachSecretMessage(marker, content) //hàm xuất xuất info box
{
		var infowindow = new google.maps.InfoWindow({ content: content, maxWidth: 500});
		
		google.maps.event.addListener(marker, 'click', function() 
		{
			infowindow.open(map,marker);
		});


}

function updateDates(which){
	var start_date = $("#date").val();
	var end_date = $("#end_date").val();
	var year = start_date.substring(0,4);
	var month = start_date.substring(5,7);
	var day = start_date.substring(8,10);
	var start = new Date(year, month-1, day);
	year = end_date.substring(0,4);
	month = end_date.substring(5,7);
	day = end_date.substring(8,10);
	var end = new Date(year, month-1, day);
	if(end.getTime() < start.getTime()){
		if(which == "start") $("#end_date").val($("#date").val());
		else $("#date").val($("#end_date").val());
	}
}

function validate(){
	$("#validationbox").hide();
	var start_date = $("#date").val();
	var end_date = $("#end_date").val();
	var year = start_date.substring(0,4);
	var month = start_date.substring(5,7);
	var day = start_date.substring(8,10);
	var start = new Date(year, month-1, day);
	year = end_date.substring(0,4);
	month = end_date.substring(5,7);
	day = end_date.substring(8,10);
	var end = new Date(year, month-1, day);
	if(end.getTime() < start.getTime()){
		$("#validationbox").css("display","").html("Your start date is after your end date.  Please fix and try again");
		return false;
	}
	var difference = (end.getTime() - start.getTime()) / (1000*60*60*24);
	if(difference > 14) return confirm("The dates you have selected make your yard sale " + difference + " days long.  Are you sure this is correct?"); 
	else return true;
}

function initialize() {
	var myOptions = {
		mapTypeId: google.maps.MapTypeId.ROADMAP,
	    zoom: 15
    }

	map = new google.maps.Map(document.getElementById("Maps"), myOptions);
}    

//check address and get GEO CODE    	
function geocode(){
	initialize();
	geocoder = new google.maps.Geocoder();
	var address = $("#fullAddress").val();
	//var address = document.getElementById("address").value;
	geocoder.geocode( { 'address': address}, function(results, status) {
		if (status == google.maps.GeocoderStatus.OK) {
			console.log(results);
			var s = "";
		    var street_number = "";
		    var route = "";
		    var locality = "";
		    var state = "";
		    var postal_code = "";
		    var comps = results[0].address_components;
			for(var j=0;j<comps.length;j++){
				var comp = comps[j];
				//street
				if(match('street_number',comp.types))
					street_number = comp.long_name;
				//street name
				if(match('route',comp.types)) 
					route = comp.long_name;
				//location
				if(match('locality',comp.types))
					locality = comp.long_name;
				if(match("sublocality",comp.types) && locality.length==0) 
					locality = comp.long_name;
				if(match("administrative_area_level_3",comp.types) && locality.length==0) 
					locality = comp.long_name;
				if(match('administrative_area_level_1',comp.types)) 
					state = comp.short_name;
				if(match('postal_code',comp.types)) 
					postal_code = comp.long_name;
				s = street_number +" "+route+", "+locality+", "+state+", "+postal_code;

		    }
		    if(route == ''){
				$("#notfound").html("Could not find address").show();
				$("#street").val("");
				$("#city").val("");
				$("#state").val("");
				$("#zip").val("");
				$("#eventDetail").hide();
				$("#address").hide();
			}else{
			    $("#notfound").hide();
		    	$("#street").val(street_number+" "+route);
		    	$("#city").val(locality);
		    	$("#state").val(state);
		    	$("#zip").val(postal_code);
		    	$("#lat").val(results[0].geometry.location.lat());
				$("#lng").val(results[0].geometry.location.lng());
		    	$("#eventDetail").show();
				
				map.setCenter(results[0].geometry.location);
				var marker = new google.maps.Marker({
					position: results[0].geometry.location, 
			        map: map,
			        title:""
			    });
				$("#Maps").show();
			    $("#address").html(s).show();	   
			}
		}else{
			$("#notfound").html("Could not find address. "+status).show();
	    	$("#address").hide();
		}
	});
}
		

function match(val, array){
	if(array){
		for(var i=0;i<array.length;i++){
			if(array[i]==val) return true;
		}
	}
	return false;
}
